<?php

namespace app\admin\model;

use think\Cookie;
use think\Model;
use think\Session;

class Login extends Model
{
    protected $table = 'admin';

    /**
     * 登录验证
     */
    public function login()
    {
        $data = input('post.');
        $adminInfo = $this->where(['user_name' => $data['user_name']])->find();
        if (!empty($adminInfo)) {
            if ($adminInfo['active'] == 0) {
                return array('error' => 1, 'msg' => '该用户已失效');
            } else {
                if (md5($data['password'].$adminInfo['salt']) != $adminInfo['password']) {
                    return array('error' => 1, 'msg' => '密码不正确');
                } else {
                    Session::set('id', $adminInfo['id']);
                    Session::set('user_name', $adminInfo['user_name']);
                    Session::set('name', $adminInfo['name']);
                    return array('error' => 0, 'msg' => '登陆成功');
                }
            }
        } else {
            return array('error' => 1, 'msg' => '用户名不存在');
        }
    }

    /**
     * 自动登录
     */
    public function autologin()
    {
        $id = Cookie::get('id');
        $logname = Cookie::get('logname');
        if (!empty($id) && !empty($logname)) {
            $info = Db::table('zy_admin')->where(['id' => $id, 'logname' => $logname])->find();
            if ($info) {
                Session::set('id', $info['id']);
                Session::set('logname', $info['logname']);
                Session::set('aname', $info['aname']);
                Session::set('role_id', $info['role_id']);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 修改密码
     */
    public function updatepwd()
    {
        $data = input('post.');
        if (empty($data['repwd'])) {
            return array('error' => 1, 'msg' => '新密码不能为空');
        } else {
            $list = Db::table('zy_admin')->where(['id' => $data['id']])->find();
            if (md5($data['pwd']) != $list['pwd']) {
                return array('error' => 1, 'msg' => '密码不正确');
            } else {
                if ($data['repwd'] != $data['repwd1']) {
                    return array('error' => 1, 'msg' => '两次输入新密码不一致');
                } else {
                    $update = array(
                        'pwd' => (md5($data['repwd']))
                    );
                    Db::table('zy_admin')->where(['id' => $list['id']])->update($update);
                    if ($list['logname'] == Session::get('logname')) {
                        return array('error' => 2);
                    } else {
                        return array('error' => 0);
                    }
                }
            }
        }

    }

    /**
     * 锁屏验证
     */
    public function lockscreen()
    {
        $data = input('post.');
        $lists = Db::table('zy_admin')->where(['logname' => input('id')])->find();
        if (md5($data['password']) == $lists['pwd']) {
            return $lists;
        } else {
            return array('error' => 1, 'msg' => '密码错误');
        }
    }

    /**
     * 重置密码
     */
    public function setnewpwd()
    {
        $data = input('post.');
        $lists = Db::table('zy_admin')->where(['email' => $data['email'], 'logname' => $data['logname']])->find();
        $repwd = rand(111111, 999999);
        if (empty($lists)) {
            return array('error' => 1, 'msg' => '账户名或邮箱错误');
        } else {
            $update = array(
                'pwd' => md5($repwd)
            );
            Db::table('zy_admin')->where(['id' => $lists['id']])->update($update);
            if ($lists['logname'] == Session::get('logname')) {
                return array('error' => 2, 'msg' => '重置成功', 'repwd' => $repwd);
            } else {
                return array('error' => 0, 'msg' => '重置成功', 'repwd' => $repwd);
            }
        }
    }
}